import axios from 'axios'
// 全局的 axios 默认值,注意连接位置为后台服务8888端口，否则提示跨域问题报错
axios.defaults.baseURL = 'http://localhost:8888/api/private/v1'
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  // 读取本地是否存在token值
  let token = localStorage.getItem('mytoken') || ''
  if (token) {
    config.headers['Authorization'] = token
  }
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error)
})

// 登录
export const postLogin = (obj) => axios.post('login', obj).then(res => res.data)
// 左侧菜单
export const getMenusList = () => axios.get('menus').then(res => res.data)
// 所有权限列表
export const getAllRightsList = (type) => axios.get(`rights/${type}`).then(res => res.data)
// 角色列表
export const getRolesList = () => axios.get('roles').then(res => res.data)
// 添加角色
export const postAddRoles = (roleName, roleDesc) => axios.post('roles', { roleName, roleDesc }).then(res => res.data)
// 删除角色
export const deleteRoles = (id) => axios.delete(`roles/${id}`).then(res => res.data)
// 编辑角色
export const putEditRoles = (id, roleName, roleDesc) => axios.put(`roles/${id}`, { roleName, roleDesc }).then(res => res.data)
// 删除角色指定权限
export const deleteRolesRights = (roleId, rightId) => axios.delete(`roles/${roleId}/rights/${rightId}`)
// 角色授权
export const postRolesAuthSubmit = (roleId, rids) => axios.post(`roles/${roleId}/rights`, { rids: rids }).then(res => res.data)
// 用户列表
export const getAdduser = (query, pagenum, pagesize) => axios.get(`users`, { params: { query, pagenum, pagesize } }).then(res => res.data)

// 添加用户
export const addUser = obj => axios.post(`users`, obj).then(res => res.data)

// 删除用户
export const editDel = id => axios.delete(`users/${id}`).then(res => res.data)

// 编辑用户
export const editUser = obj => axios.put(`users/${obj.id}`, { email: obj.email, mobile: obj.mobile }).then(res => res.data)

// 切换用户状态
export const changeState = (id, state) => axios.put(`users/${id}/state/${state}`).then(res => res.data)
// 获取订单数据列表
export const getOrderList = obj => axios.get('/orders', { params: obj }).then(res => res.data)
// 商品列表数据
export const getGoodsList = (query, pagenum, pagesize) => axios.get('goods', { params: { query, pagenum, pagesize } }).then(res => res.data)
// 商品删除
export const deleteGoods = (id) => axios.delete(`goods/${id}`).then(res => res.data)
// 商品分类列表
export const getGoodsCategories = (type, pagenum, pagesize) => axios.get('categories', { params: { type, pagenum, pagesize } }).then(res => res.data)
// 添加商品分类
export const postGoodsCategories = (obj) => axios.post('categories', obj).then(res => res.data)
// 根据id查询分类
export const getCatById = (id) => axios.get(`categories/${id}`).then(res => res.data)
// 编辑分类提交
export const putEditCatsubmit = (id, cname) => axios.put(`categories/${id}`, { cat_name: cname }).then(res => res.data)
// 添加商品
export const postAddGoods = (obj) => axios.post('goods', obj).then(res => res.data)
// 参数列表
export const getCatAttributes = (id, sel) => axios.get(`categories/${id}/attributes`, { params: { sel: sel } }).then(res => res.data)
